package com.example.opengldemo.mbo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLES30;
import android.opengl.GLSurfaceView;
import android.opengl.GLUtils;

import com.example.opengldemo.R;

import com.example.opengldemo.util.MatrixState;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class MyEarthAndMoonSystem extends GLSurfaceView implements GLSurfaceView.Renderer {


    EarthDemo earthDemo ;

    public MyEarthAndMoonSystem(Context context) {
        super(context);
        initConfig() ;
    }

    private void initConfig() {
        setEGLContextClientVersion(3);
        setRenderer(this);
        this.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
    }

    @Override
    public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
        GLES30.glClearColor(0, 0, 0, 1f);
        GLES30.glEnable(GLES30.GL_DEPTH_TEST);
        earthDemo = new EarthDemo(this);

        MatrixState.setInitStack();
    }
    int dayHandle ;
    int nightHandle ;
    int textureIdCloud;
    @Override
    public void onSurfaceChanged(GL10 gl10, int i, int i1) {
        GLES30.glViewport( 0 , 0  , i , i1);
        float ratio = (float)i/i1 ;

        MatrixState.setProjectFrustum(-ratio, ratio, -1, 1, 4f, 100);
        MatrixState.setCamera(0,0,20f,0f,0f,0f,0f,1.0f,0.0f);

        Bitmap bitmap = initBitmap(R.drawable.earth);
        dayHandle = initTexture(bitmap);
        bitmap.recycle();

        bitmap = initBitmap(R.drawable.earthn);
        nightHandle = initTexture(bitmap);
        bitmap.recycle();

        bitmap = initBitmap(R.drawable.cloud);
        textureIdCloud = initTexture(bitmap);
        bitmap.recycle();



        MatrixState.setLightLocation(100f,5f,0); //设置光源位置

    }

    @Override
    public void onDrawFrame(GL10 gl10) {
        GLES30.glClear(GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
        MatrixState.pushMatrix();
        MatrixState.rotate(MatrixState.rotate[0], 1, 0, 0);
        MatrixState.rotate(MatrixState.rotate[1], 0, 1, 0);
        MatrixState.rotate(MatrixState.rotate[2], 0, 0, 1);
        MatrixState.translate(MatrixState.translate[0], MatrixState.translate[1], MatrixState.translate[2]);

        earthDemo.drawSelf(dayHandle , nightHandle);

        MatrixState.popMatrix();

    }

    public int initTexture(Bitmap bitmap){
        int[] textureArray = new int[1];
        GLES30.glGenTextures(1 , textureArray , 0);
        int textureid = textureArray[0];
        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D , textureid);
        GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D , GLES30.GL_TEXTURE_MIN_FILTER , GLES30.GL_NEAREST);
        GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D , GLES30.GL_TEXTURE_MAG_FILTER , GLES30.GL_LINEAR);
        GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D , GLES30.GL_TEXTURE_WRAP_S , GLES30.GL_CLAMP_TO_EDGE);
        GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D , GLES30.GL_TEXTURE_WRAP_T , GLES30.GL_CLAMP_TO_EDGE);
        GLUtils.texImage2D(GLES30.GL_TEXTURE_2D , 0 , bitmap , 0);
        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D , 0);
        return textureid;
    }
    public Bitmap initBitmap(int res){
       return BitmapFactory.decodeResource(getResources()  , res);
    }
}
